//
// Created by XiaoXiu on 2025/4/29.
//

#include "atm_system.h"
#include <stdbool.h>
#include <stdio.h>
#include <time.h>


bool verify_account(int id, const char *password);


void start() {
    int is_running = 1;
    while (is_running) {
        printf("Welcome to ATM System!\n");
        printf("Please select your operation:\n");
        printf("1. Login\n");
        printf("2. Register\n");
        printf("3. Exit\n");
        printf("Your choice: ");
        int choice;
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                login_account();
                break;
            case 2:
                register_account();
                break;
            case 3:
                is_running = 0;
                break;
            default:
                printf("Invalid choice!\n");
                break;
        }
    }
    printf("Thank you for using ATM System!\n");
}

void register_account() {
    int is_running = 1;
    while (is_running) {
        printf("Please enter your accountName: ");
        char name[32];
        scanf("%s", name);
        account *current_account, *tmp;

        int account_is_valid = 1;

        HASH_ITER(hh, accounts, current_account, tmp) {
            if (strcmp(current_account->name, name) == 0) {
                printf("AccountName already exists!\n");
                account_is_valid = 0;
            }
        }

        if (!account_is_valid) {
            continue;
        }

        printf("Please enter your password: ");
        char password[32];
        scanf("%s", password);

        srand(time(NULL));
        // 生成随机 id
        int id = rand() % (1999999 - 1000000 + 1) + 1000000;
        account *na = create_account(id, name, password, 0);
        add_account(na);
        printf("Your account has been created successfully!\n");
        printf("Your account ID is %d\n", id);
        is_running = 0;
        break;
    }
}


void login_account() {
    int is_running = 1;
    while (is_running) {
        printf("Please enter your account ID: ");
        int id;
        scanf("%ld", &id);
        printf("Please enter your password: ");
        char password[32];
        scanf("%s", password);
        if (verify_account(id, password)) {
            printf("Login successful!\n");
            account *account = find_account(id);
            account_home(account);
            is_running = 0;
            break;
        }
        printf("Login failed!\n");
        is_running = 0;
        break;
    }
}

void account_home(account *current_account) {
    int is_running = 1;
    while (is_running) {
        printf("Hello, %s!\n", current_account->name);
        printf("Please select your operation:\n");
        printf("1. Deposit\n");
        printf("2. Withdraw\n");
        printf("3. Transfer\n");
        printf("4. Info\n");
        printf("5. Logout\n");

        int choice;
        printf("Your choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1:
                deposit(current_account);
                break;
            case 2:
                withdraw(current_account);
                break;
            case 3:
                transfer(current_account);
                break;
            case 4:
                info(current_account);
                break;
            case 5:
                login_out(current_account);
                return;
            default:
                printf("Invalid choice!\n");
                break;
        }
    }
}

void deposit(account *current_account) {
    //input amount
    printf("Please enter the amount you want to deposit: ");
    double amount;

    scanf("%lf", &amount);

    if (amount <= 0) {
        printf("Invalid amount!\n");
        return;
    }

    if (amount > 1000000) {
        printf("Invalid amount!\n");
        return;
    }

    current_account->balance += amount;
    printf("Deposit successful!\n");
    printf("Your balance is %.2lf\n", current_account->balance);
}

void withdraw(account *current_account) {
    printf("Please enter the amount you want to withdraw: ");
    double amount;
    scanf("%lf", &amount);

    if (amount <= 0) {
        printf("Invalid amount!\n");
        return;
    }
    if (amount > current_account->balance) {
        printf("Insufficient balance!\n");
        return;
    }
    current_account->balance -= amount;
    printf("Withdraw successful!\n");
    printf("Your balance is %.2lf\n", current_account->balance);
}

//login_out
void login_out(account *account) {
    printf("Logout successful!\n");
    printf("Goodbye! %s\n", account->name);
}

//info
void info(account *account) {
    printf("Your account information is as follows:\n");
    printf("Your account ID is %d\n", account->id);
    printf("Your balance is %.2lf\n", account->balance);
    printf("Your account name is %s\n", account->name);
}

//transfer
void transfer(account *current_account) {
    int is_running = 1;
    while (is_running) {
        printf("Please enter the account ID you want to transfer to: ");
        int id;
        scanf("%d", &id);
        account *fa = find_account(id);
        if (fa == NULL) {
            printf("Account not found!\n");
            continue;
        }
        printf("Please enter the amount you want to transfer: ");
        double amount;
        scanf("%lf", &amount);
        if (amount <= 0) {
            printf("Invalid amount!\n");
            continue;
        }
        if (amount > current_account->balance) {
            printf("Insufficient balance!\n");
            continue;
        }
        fa->balance += amount;
        current_account->balance -= amount;
        printf("Transfer successful!\n");
        printf("Your balance is %.2lf\n", current_account->balance);
        is_running = 0;
        break;
    }
}


bool verify_account(const int id, const char *password) {
    const account *fd = find_account(id);
    return fd != NULL && strcmp(fd->password, password) == 0;
}
